package cn.itsource.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		String pwd = req.getParameter("pwd");
		//判断
		if("tom".equals(name) && "123456".equals(pwd)){//登录成功
			//获取session对象
			HttpSession session = req.getSession();
			//设置session对象的过期时间为30s
			session.setMaxInactiveInterval(30);
			//在session中绑定用户名
			session.setAttribute("name", name);
			//重定向
			resp.sendRedirect(resp.encodeRedirectURL("success.jsp"));
		}else{
			//登录失败 -- 回到登录页面重新登录
			resp.sendRedirect("index.jsp");
		}
	}
}
